//
//  ZLQThirdPageTC.swift
//  GoTravel
//
//  Created by lanou3g on 16/3/9.
//  Copyright © 2016年 SHH. All rights reserved.
//

import UIKit

class ZLQThirdPageTC: UITableViewController {

    var upV = UIView()
    
    var doneV = UIView()
    
    var pickerV = UIImageView()
    
    var isAAA:Bool = true
    
    var leftB:UINavigationItem?
    
    var aStrId = Int()
    
    var numB:Int?
    var a:Int?
    
    var string = String() //上个页面传过来的name
    
    let urlString1 = "http://q.chanyouji.com/api/v1/inspiration_activities/"
    let urlString2 = ".json"
    
    let urlstring = "http://q.chanyouji.com/api/v1/activity_collections/5.json"
    
    var dataArray = Array<ZLQThirdModel>()
    var dataArray1 = Array<ZLQInfoDoneViewModel>()
    
    var addressL:UILabel?
    
    var index:Int?
    var aaaID:Int?
    override func viewDidLoad() {
        super.viewDidLoad()

        self.view.backgroundColor = UIColor.whiteColor()
        self.tableView.registerClass(ZLQThirdPageCell.classForCoder(), forCellReuseIdentifier: "cell")
        
        self.navigationItem.title = self.string
//        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: Selector("bakeAction"))
        
        dataAnalysis1()
//        dataAnalysis()

    }
    func dataAnalysis1() {
    
        let str = "http://q.chanyouji.com/api/v1/activity_collections/" + String(aStrId) + ".json"
        let url = NSURL(string: str)
        let session = NSURLSession.sharedSession()
        let task = session.dataTaskWithURL(url!) { (data:NSData?, re:NSURLResponse?, error:NSError?) -> Void in
            
            do {
            
                let dic:NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as! NSDictionary
                let arr:NSArray = dic["data"] as! NSArray
                for dict in arr {
                
                    let model = ZLQThirdModel()
                    model.setValuesForKeysWithDictionary(dict as! [String : AnyObject])
                    self.dataArray.append(model)
                    
                }
                
                
            } catch {}
            
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                self.tableView.reloadData()
            })
        }
        
        task.resume()
    }
    

    func dataAnalysis() {
    
        let string:String = urlString1 + String(aaaID!) + urlString2
        let url = NSURL(string: string)
        print(url)
        let session = NSURLSession.sharedSession()
        let task1 = session.dataTaskWithURL(url!) { (data:NSData?, response:NSURLResponse?, error:NSError?) -> Void in
            print(data!)
            
            do {
                let dic:NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments) as! NSDictionary
//                for dict1 in dic["data"] as! NSDictionary {
//                print(dic["data"])
                   let dict = dic["data"] as! NSDictionary
                    let model = ZLQInfoDoneViewModel()
                    model.setValuesForKeysWithDictionary(dict as! [String : AnyObject])
                    self.dataArray1.append(model)
                    print("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\(self.dataArray1.count)")
                   self.addressL?.text = model.tip
//                }

            } catch {
            }
            dispatch_async(dispatch_get_main_queue(), { () -> Void in
                
                self.tableView.reloadData()
            })
//
        }
        task1.resume()
    }
    
//    func bakeAction() {
//    
//        
//        UIView.beginAnimations(nil, context: nil)
//        UIView.setAnimationDuration(0.6)
//        UIView.setAnimationDelegate(self)
//        upV.alpha = 0
//        doneV.alpha = 0
//        upV.frame = CGRectMake(self.view.frame.size.width, 0, 0, self.view.frame.size.height*0.25)
//        doneV.frame = CGRectMake(self.view.frame.size.width,self.view.frame.size.height*0.25,0,view.frame.size.height*0.67)
//        UIView.commitAnimations()
//        
////        upV.removeFromSuperview()
////        doneV.removeFromSuperview()
//
//       
//    
//    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    // MARK: - Table view data source

    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        // #warning Incomplete implementation, return the number of sections
        return 1
    }

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of rows
        return self.dataArray.count
    }
    
    override func tableView(tableView: UITableView, heightForRowAtIndexPath indexPath: NSIndexPath) -> CGFloat {
        return self.view.frame.size.height*0.07
    }
   
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier("cell", forIndexPath: indexPath) as? ZLQThirdPageCell
        
        let model:ZLQThirdModel = self.dataArray[indexPath.row]
        cell?.nameL?.text = model.topic

        return cell!
    }
    

 
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
      
        
        let a = ZLQWebViewController()
        a.aaInt = self.dataArray[indexPath.row].aId!
        self.navigationController?.pushViewController(a, animated: true)
        
//          addInfoViews()

        
//        self.index = indexPath.row
//        self.aaaID = self.dataArray[indexPath.row].aId!
//        dataAnalysis()
//        upV.frame =  CGRectMake(0,0,self.view.frame.size.width,0)
//        upV.backgroundColor = UIColor.redColor()
//        doneV.frame = CGRectMake(0,self.view.frame.size.height,self.view.frame.size.width,0)
//        doneV.backgroundColor = UIColor.yellowColor()
//        self.view.addSubview(upV)
//        self.view.addSubview(doneV)
//        pickerV = UIImageView(frame: CGRectMake(0,0,self.view.frame.size.width,0))
//        pickerV.image = UIImage(named:"62fb7cc05fc0058bbed6317f30921f83")
//        upV.addSubview(pickerV)
//        
//    
////        let model:ZLQInfoDoneViewModel = self.dataArray1[indexPath.row]
////        let model:ZLQInfoDoneViewModel = ZLQInfoDoneViewModel()
//        addressL = UILabel(frame: CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height*0.67))
//        addressL?.backgroundColor = UIColor.redColor()
////        addressL?.text = model.topic!
//        doneV.addSubview(addressL!)
//            dataAnalysis()
//        
//        UIView.beginAnimations(nil, context: nil)
//        UIView.setAnimationDuration(0.6)
//        UIView.setAnimationDelegate(self)
//        upV.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height*0.25)
//        pickerV.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height*0.25)
//        doneV.frame = CGRectMake(0,self.view.frame.size.height*0.25,self.view.frame.size.width,view.frame.size.height*0.67)
//        upV.alpha = 1
//        doneV.alpha = 1
//
//        UIView.commitAnimations()
//        
//        self.tableView.reloadData()
//        
//        let model:ZLQInfoDoneViewModel = self.dataArray1[indexPath.row]
//        
//        addressL = UILabel(frame: self.doneV.bounds)
//        addressL?.text = model.tip
//        doneV.addSubview(addressL!)
      
 
    }
//    
//    func addInfoViews() {
//    
////         dataAnalysis()
//        
//        upV.frame =  CGRectMake(0,0,self.view.frame.size.width,0)
//        upV.backgroundColor = UIColor.redColor()
//        doneV.frame = CGRectMake(0,self.view.frame.size.height,self.view.frame.size.width,0)
//        doneV.backgroundColor = UIColor.yellowColor()
//        self.view.addSubview(upV)
//        self.view.addSubview(doneV)
//        pickerV = UIImageView(frame: CGRectMake(0,0,self.view.frame.size.width,0))
//        pickerV.image = UIImage(named:"62fb7cc05fc0058bbed6317f30921f83")
//        upV.addSubview(pickerV)
//        
//        
//        let model:ZLQInfoDoneViewModel = self.dataArray1[self.index!]
//        
//        addressL = UILabel(frame: self.doneV.bounds)
//        addressL?.text = model.tip!
//        doneV.addSubview(addressL!)
//        
//        
//       
//    }
    
    
    /*
    // Override to support conditional editing of the table view.
    override func tableView(tableView: UITableView, canEditRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return false if you do not want the specified item to be editable.
        return true
    }
    */

    /*
    // Override to support editing the table view.
    override func tableView(tableView: UITableView, commitEditingStyle editingStyle: UITableViewCellEditingStyle, forRowAtIndexPath indexPath: NSIndexPath) {
        if editingStyle == .Delete {
            // Delete the row from the data source
            tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: .Fade)
        } else if editingStyle == .Insert {
            // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
        }    
    }
    */

    /*
    // Override to support rearranging the table view.
    override func tableView(tableView: UITableView, moveRowAtIndexPath fromIndexPath: NSIndexPath, toIndexPath: NSIndexPath) {

    }
    */

    /*
    // Override to support conditional rearranging of the table view.
    override func tableView(tableView: UITableView, canMoveRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        // Return false if you do not want the item to be re-orderable.
        return true
    }
    */

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
